package com.neusoft.boot32.dibuy.tools;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

/**
 * @program: springboot32
 * @description:
 * @author: Jingdi.Duan ,duanjingdi@msn.com
 * @create: 2024-07-11 13:52
 **/

public class CodeGenerator {
    public static void main(String[] args) {
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/gate?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai", "root", "root")
                .globalConfig(builder -> {
                    builder.author("Jingdi.Duan,duanjingdi@msn.com") // 设置作者

//                            .enableSwagger() // 开启 swagger 模式
                            // 开启 SpringDoc 模式
                            .enableSpringdoc()

                            .outputDir("D:\\Gitee\\springboot32\\bootweb\\a\\src\\main\\java"); // 指定输出目录
                })
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder ->
                        builder.parent("com.neusoft.boot32.dibuy") // 设置父包名
                                .service("repository")
                                .serviceImpl("repository.impl")

                                .pathInfo(Collections.singletonMap(OutputFile.xml, "D:\\Gitee\\springboot32\\bootweb\\a\\src\\main\\resources\\mapper")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude("fa_role") // 设置需要生成的表名
                                .addTablePrefix("fa_", "c_") // 设置过滤表前缀
                                .entityBuilder()
                                .enableLombok()
                                .enableColumnConstant()
                                .javaTemplate("templates/entity.java")
                                .superClass("com.neusoft.boot32.dibuy.entity.BaseEntity")
                                .enableTableFieldAnnotation()
                                .addIgnoreColumns("autoid" ,"status","create_time","last_modified_time","version")
                                .enableTableFieldAnnotation()
                                .controllerBuilder()
                                .enableRestStyle() // 启用 REST 风格

                )
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板

                .execute();
    }
}
